package com.example.music.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.music.service.BaseService;

import java.util.List;
import java.util.Map;

public abstract class BaseServiceImpl<T, M extends BaseMapper<T>> implements BaseService<T> {
    protected abstract M getMapper();

    @Override
    public Integer add(T entity) {
        return getMapper().insert(entity);
    }

    @Override
    public Integer deleteById(String id) {
        return getMapper().deleteById(id);
    }

    @Override
    public void deleteByIds(String ids) {
        String[] idStrs = ids.split(",");
        for (String idStr : idStrs) {
            deleteById(idStr);
        }
    }

    @Override
    public void deleteByParam(String key, Object value) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(key,value);
        getMapper().delete(queryWrapper);
    }

    @Override
    public void deleteByParams(Map<String, Object> params) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            queryWrapper.eq(param.getKey(), param.getValue());
        }
        getMapper().delete(queryWrapper);
    }

    @Override
    public int update(T entity, UpdateWrapper<T> updateWrapper) {
        return getMapper().update(entity, updateWrapper);
    }

    @Override
    public List<T> selectAll() {
        return getMapper().selectList(null);
    }

    @Override
    public T selectItemById(String id) {
        return getMapper().selectById(id);
    }

    @Override
    public List<T> selectItemsByParam(String key, Object value) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(key, value);
        return getMapper().selectList(queryWrapper);
    }

    @Override
    public List<T> selectItemsByParams(Map<String, Object> params) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            queryWrapper.eq(param.getKey(), param.getValue());
        }
        return getMapper().selectList(queryWrapper);
    }

    @Override
    public Page<T> pageTest(String id){
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        List<T> ts = getMapper().selectList(queryWrapper);
        Page<T> tPage = new Page<>();
        return tPage;
    }
}
